﻿using GameBasic;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

namespace VCity
{
    /// <summary>
    /// Cursor state stacks
    /// </summary>
    public class InputMain : MonoSingleton<InputMain>
    {

        public PlayerInput playerInput;


        internal List<CursorState> cursorStates;

        protected override void Awake()
        {
            base.Awake();

            Cursor.lockState = CursorLockMode.Locked;
            cursorStates = new List<CursorState>();
            AddCursorState(new CursorState() {
                visible = true,
                lockMode = CursorLockMode.None
            });
        }

        private void Update()
        {
            if (cursorStates.Count > 0)
            {
                var state = cursorStates[cursorStates.Count - 1];
                Cursor.visible = state.visible;
                Cursor.lockState = state.lockMode;
            }
        }

        public void AddCursorState(CursorState state)
        {
            cursorStates.Add(state);
        }

        public void RemoveCursorState(CursorState state)
        {
            for (int i = cursorStates.Count - 1; i >= 0; i--)
            {
                if (cursorStates[i] == state)
                {
                    cursorStates.RemoveAt(i);
                }
            }
        }
    }

    public class CursorState
    {
        public bool visible;
        public CursorLockMode lockMode;
    }
}